package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domin.pojo.Clazz;
import com.itheima.domin.query.ClazzQuery;
import com.itheima.domin.vo.PageResult;
import com.itheima.exception.ClientException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.service.ClazzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**班级业务处理
 * @Author:上官徵羽
 * @Date: 2025/07/11 18:13
 * @Description TODO 今日内容:
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    private static final Logger log = LoggerFactory.getLogger(ClazzServiceImpl.class);
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Clazz> select(){
        return clazzMapper.select();
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    //@Transactional(rollbackFor = Exception.class)
    public void deleteClazzIds(Integer id) {
        if (CollUtil.isEmpty(Collections.singleton(id)))
        {return;}
        log.info("ClazzServiceImpl deleteClazzIds:{}",id);
        clazzMapper.deleteClazzIds(id);
    }

    /**
     * 班级数据回显
     * @param id
     * @return
     */
    @Override
    public Clazz selectClazzId(Integer id) {
        return clazzMapper.selectClazzId(id);
    }

    /**
     * 修改班级数据
     * @param clazz
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClazzId(Clazz clazz) {
        Clazz clazz1 = clazzMapper.selectClazzId(clazz.getId());
        if (!StrUtil.equals(clazz1.getName(),clazz.getName())){
            if (clazzMapper.clazzNameCount(clazz.getName())>0){
                throw new ClientException(clazz.getName()+"用户名已存在,请更换");
            }
        }
        LocalDateTime now = LocalDateTime.now();
        clazz.setUpdateTime(now);
        clazzMapper.updateClazzId(clazz);
    }

    /**
     * 分页查询
     * @param clazzQuery
     * @return
     */
    @Override
    public PageResult<Clazz> findAll(ClazzQuery clazzQuery) {
        PageHelper.startPage(clazzQuery.getPage(),clazzQuery.getPageSize());
        Page<Clazz> p = clazzMapper.findAll(clazzQuery);
        /** 调用持久层 */
        return new PageResult<>(p.getTotal(),p.getResult());
    }


    /**
     * 新增班级
     * @param clazz
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addClazzAndMaster(Clazz clazz) {
        // 添加班级
        LocalDateTime now = LocalDateTime.now();
        clazz.setCreateTime(now);
        clazz.setUpdateTime(now);
        clazz.setMasterId(clazz.getMasterId());
        clazzMapper.addClazz(clazz);
    }
}
